package org.example.sdpback.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.example.sdpback.domain.entity.User;
import org.example.sdpback.domain.po.UserPO;
import org.example.sdpback.domain.vo.UserVO;
import org.example.sdpback.sdk.response.SdpResponseData;
import org.example.sdpback.service.UserService;
import org.example.sdpback.utils.JwtUtil;
import org.example.sdpback.utils.ThreadLocalUtil;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Tag(name="用户管理模块")
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;

    @GetMapping("/getUser")
    @Operation(summary = "获取用户信息")
    public SdpResponseData<User> getUser() {
        User userTokenData = ThreadLocalUtil.get();
        User user = userService.getUserById(userTokenData.getId());
        return SdpResponseData.success(user);
    }

    @GetMapping("/getUser/list")
    @Operation(summary = "获取用户列表")
    public SdpResponseData<List<User>> getUserList(String username) {
        List<User> users = userService.getUserList(username);
        return SdpResponseData.success(users);
    }


    @PostMapping("/register")
    @Operation(summary = "用户注册")
    @Parameters({
            @Parameter(name = "username", description = "用户名", required = true),
            @Parameter(name = "password", description = "密码", required = true)
    })
    public SdpResponseData<Object> register(String username, String password) {
        //1.获取用户
        User user = userService.getUserByName(username);
        //2.判断用户名是否已存在
        if (user == null) {
            userService.register(username, password);
        }else {
            return SdpResponseData.success("用户已存在！");
        }
        return SdpResponseData.success("注册成功！");
    }

    @PostMapping("/login")
    @Operation(summary = "用户登录")
    @Parameters({
            @Parameter(name = "username", description = "用户名", required = true),
            @Parameter(name = "password", description = "密码", required = true)
    })
    public SdpResponseData<UserVO> login(String username, String password) {
        return SdpResponseData.success(userService.login(username, password));
    }

    @PostMapping("/modify/password")
    @Operation(summary = "修改密码")
    @Parameter(name = "password", description = "密码", required = true)
    public SdpResponseData<Object> modifyPassword(String password) {
        //获取用户
        User user = ThreadLocalUtil.get();
        //更新密码
        userService.modifyPassword(user, password);
        return SdpResponseData.success();
    }

    @PostMapping("/init/password")
    @Operation(summary = "初始化密码")
    @Parameter(name = "username", description = "用户名", required = true)
    public SdpResponseData<Object> initPassword(String username) {
        //获取用户
        User user = userService.getUserByName(username);
        if (user == null){
            return SdpResponseData.success("用户不存在！");
        }
        //更新密码
        userService.initPassword(user);
        return SdpResponseData.success();
    }

    @PostMapping("/update")
    @Operation(summary = "更新用户信息")
    public SdpResponseData<Object> updateUser(@RequestBody UserPO userPO) {
        // 获取去当前用户
        User tokenUser = ThreadLocalUtil.get();
        //校验是否为当前用户
        userService.checkCurrentUser(userPO, tokenUser);
        return SdpResponseData.success();
    }
}
